-- frame_mgr.lua
-- Created by shenyq Sep/06/2012
-- 帧管理器

declare_module("FRAME_MGR")

local m_frame_listeners = {}
local m_timer_listeners = {}

function register_frame_listener(listener)
    table.insert(m_frame_listeners, listener);
end

function unregister_frame_listener(listener)
    array.remove(m_frame_listeners, listener);
end

function register_timer_listener(listener, interval)
    local timer = {
        listener,   -- 监听器
        interval,   -- 周期
        0,          -- 当前时间
    }
    table.insert(m_timer_listeners, timer);
end

function unregister_timer_listener(listener)
    array.remove(m_timer_listeners, listener);
end

function update(time)
    for _, v in ipairs(m_frame_listeners) do
        safe_call(v.frame_update, v, time);
    end

    for _, v in ipairs(m_timer_listeners) do
        local listener, interval, life = unpack(v);
        life = life + time;
        if life >= interval then
            safe_call(listener.timer_update, listener, life);
            life = 0;
        end
        v[3] = life;
    end
end

